<?php

/*
 * Copyright 2008 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#namespace tehframework\iocc;

class ObjectDefinition
{
	/**
	 * @var string
	 */
	private $class;
	
	/**
	 * @var array<int,mixed>
	 */
	private $arguments = array();
	
	/**
	 * @var array<string,mixed>
	 */
	private $fields = array();
	
	/**
	 * @var array<MethodCall>
	 */
	private $methodCalls = array();
	
	/**
	 * @param  string $class
	 */
	public function __construct($class)
	{
		$this->class = $class;
	}
	
	/**
	 * @return string
	 */
	public function getClass()
	{
		return $this->class;
	}
	
	/**
	 * @param  array<int,mixed>? $resolveArgs
	 * @return array<int,mixed>
	 */
	public function getArguments(array $resolveArgs = null)
	{
		if (empty($resolveArgs))
		{
			return $this->arguments;
		}
		
		return $resolveArgs + $this->arguments;
	}
	
	/**
	 * @param  array<int,mixed> $arguments
	 * @return ObjectDefinition
	 */
	public function setArguments(array $arguments)
	{
		$this->arguments = $arguments;
		
		return $this;
	}
	
	/**
	 * @param  int? $pos
	 * @param  mixed $value
	 * @return ObjectDefinition
	 */
	public function setArgument($pos, $value)
	{
		if ($pos === null)
		{
			$this->arguments[] = $value;
		}
		else
		{
			$this->arguments[$pos] = $value;
		}
		
		return $this;
	}
	
	/**
	 * @return array<string,mixed>
	 */
	public function getFields()
	{
		return $this->fields;
	}
	
	/**
	 * @param  array<string,mixed> $arguments
	 * @return ObjectDefinition
	 */
	public function setFields(array $fields)
	{
		$this->fields = $fields;
		
		return $this;
	}
	
	/**
	 * @param  string $name
	 * @param  mixed $value
	 * @return ObjectDefinition
	 */
	public function setField($name, $value)
	{
		$this->fields[$name] = $value;
		
		return $this;
	}
	
	/**
	 * @return array<MethodCall>
	 */
	public function getMethodCalls()
	{
		return $this->methodCalls;
	}
	
	/**
	 * @param  array<MethodCall> $methodCalls
	 * @return ObjectDefinition
	 */
	public function setMethodCalls(array $methodCalls)
	{
		$this->methodCalls = $methodCalls;
		
		return $this;
	}
	
	/**
	 * @param  MethodCall $methodCall
	 * @return ObjectDefinition
	 */
	public function addMethodCall(MethodCall $methodCall)
	{
		$this->methodCalls[] = $methodCall;
		
		return $this;
	}
}